package com.fufufu.mail.product.service.impl;

import com.fufufu.mail.product.vo.Images;
import com.fufufu.mail.product.vo.Skus;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fufufu.common.utils.PageUtils;
import com.fufufu.common.utils.Query;

import com.fufufu.mail.product.dao.SkuImagesDao;
import com.fufufu.mail.product.entity.SkuImagesEntity;
import com.fufufu.mail.product.service.SkuImagesService;
import org.springframework.util.StringUtils;


@Service("skuImagesService")
public class SkuImagesServiceImpl extends ServiceImpl<SkuImagesDao, SkuImagesEntity> implements SkuImagesService {

    @Override
    public void savaImages(Map<Long, Skus> idSkuMapping) {
        Set<Map.Entry<Long, Skus>> entries = idSkuMapping.entrySet();
        List<SkuImagesEntity> skuImagesEntities = new ArrayList<>();
        for (Map.Entry<Long, Skus> entry : entries) {
            Long skuId = entry.getKey();
            List<Images> images = entry.getValue().getImages();
            for (Images image : images) {
                // 因为前端把所有的图片都传过来了，但只有选中的图片的url地址是有的
                if (StringUtils.hasText(image.getImgUrl())) {
                    SkuImagesEntity skuImagesEntity = new SkuImagesEntity();
                    BeanUtils.copyProperties(image, skuImagesEntity);
                    skuImagesEntity.setSkuId(skuId);
                    skuImagesEntities.add(skuImagesEntity);
                }
            }
        }
        this.saveBatch(skuImagesEntities);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuImagesEntity> page = this.page(
                new Query<SkuImagesEntity>().getPage(params),
                new QueryWrapper<SkuImagesEntity>()
        );

        return new PageUtils(page);
    }

}